package com.b511.swpu.cementbackend.util;


import java.io.File;
import java.util.Arrays;
import com.alibaba.fastjson.JSON;

public class FileUtil {
    StringBuilder buf=new StringBuilder();

    public String scan(String path) {
//        if(Validate.isNull(path)){
//            return null;
//        }
        File f = new File(path);
        if (f.isDirectory())
        {
            scan(new File(path));
            buf.delete(buf.length() - 2, buf.length());
        }
        else {
            System.out.format("{\"label\" : \"%s\"}", f.getName());
        }
        return buf.toString();
    }

    private void scan(File f) {
        if (f.isDirectory())
        {
            if(f.listFiles().length>0) {
                buf.append("{").append("\"label\" : \"").append(f.getName()).append("\",").append("\"filePath\":\"\",").append("\"fileParent\":").append(JSON.toJSONString(f.getAbsolutePath().replace("C:\\WellDoc\\", ""))).append(",").append("\"isDir\":\"0\",").append("\"children\":[");
                Arrays.asList(f.listFiles()).forEach(this::scan);
                if (buf.toString().endsWith("\"children\":["))
                    buf.append("{\"label\":\"\",},");
                buf.delete(buf.length() - 2, buf.length());
                buf.append("}").append("]").append("},");
            }else {
                buf.append("{").append("\"label\" : \"").append(f.getName()).append("\",").append("\"filePath\":\"\",").append("\"fileParent\":").append(JSON.toJSONString(f.getAbsolutePath().replace("C:\\WellDoc\\", ""))).append(",").append("\"isDir\":\"0\",").append("\"children\":[");
                Arrays.asList(f.listFiles()).forEach(this::scan);
                if (buf.toString().endsWith("\"children\":["))
                buf.append("]").append("},");
            }
        }
        else  buf.append("{").append("\"label\" : \"").append(f.getName()).append("\"").append(",\"filePath\":").append(JSON.toJSONString(f.getAbsolutePath())).append(",\"isDir\":\"1\"").append("},");
    }







//    public static List<WellBrowseOutDto> createTree(File node, List<WellBrowseOutDto> list){

//        //是文件，保存文件名
//        if(node.isFile()){
//            TreeStr+="{";
//            TreeStr+="\"label\":" + "\"" + node.getName() + "\"";
//            TreeStr+="},";
//        }
//
//        //是目录(下面有文件)
//        if(node.isDirectory()){
//            TreeStr+="{";
//            TreeStr+="\"label\":" + "\"" + node.getName() + "\",";
//            TreeStr+="\"children\":[";
//            for(String filename:node.list()){
//                TreeStr+="{\"label\":" + "\"" + filename + "\"";
//                TreeStr+="},";
//                TreeStr=createTree(new File(node,filename),TreeStr);
//                System.out.println(TreeStr);
//            }
//            TreeStr+="]}";
//        }
//        WellBrowseOutDto wellBrowseOutDto=new WellBrowseOutDto();
//        wellBrowseOutDto.setLabel(node.getName());
//        if(node.isFile()){
//            list.add(wellBrowseOutDto);
//        } else{
//            for(String filename:node.list()){
//                WellBrowseOutDto child=new WellBrowseOutDto();
//                child.setLabel(filename);
//                child.setChildren(createTree(new File(node,filename),list));
//                list.add(child);
//            }
//        }
//
//
//        return list;
//
//    }

        /**
         * 删除单个文件
         * @param   sPath    被删除文件的文件名
         * @return 单个文件删除成功返回true，否则返回false
         */
        public boolean deleteFile(String sPath) {
            boolean flag = false;
            File file = new File(sPath);
            // 路径为文件且不为空则进行删除
            if (file.isFile() && file.exists()) {
                file.delete();
                flag = true;
            }
            return flag;
        }

        //删除文件夹
        public static void deleteDir(String dirPath)
        {
            File file = new File(dirPath);
            if(file.isFile())
            {
                file.delete();
            }else
            {
                File[] files = file.listFiles();
                if(files == null)
                {
                    file.delete();
                }else
                {
                    for (int i = 0; i < files.length; i++)
                    {
                        deleteDir(files[i].getAbsolutePath());
                    }
                    file.delete();
                }
            }
        }

    }
